package org.wh1tew0lf.vframe.framework;

import java.util.Map;

import org.wh1tew0lf.vframe.framework.vframeexception.VFieldNameIsNullException;
import org.wh1tew0lf.vframe.framework.vframeexception.VFrameException;

public class VDatabaseField implements IDatabaseRestriction {

	private String fieldName = null;
	private VDatabaseFieldOperator operator = null;

	public static VDatabaseField field(String fieldName) throws VFrameException {
		return new VDatabaseField(VDatabaseFieldOperator.NULL, fieldName);
	}

	public static VDatabaseField oracleTextScoreField(String fieldName)
			throws VFrameException {
		return new VDatabaseField(VDatabaseFieldOperator.ORACLE_TEXT_SCORE,
				fieldName);
	}

	public static VDatabaseField fieldWithOperator(
			VDatabaseFieldOperator operator, String fieldName)
			throws VFrameException {
		return new VDatabaseField(operator, fieldName);
	}

	private VDatabaseField(VDatabaseFieldOperator operator, String fieldName)
			throws VFrameException {
		if (operator == null) {
			operator = VDatabaseFieldOperator.NULL;
		}
		if (fieldName == null) {
			throw new VFieldNameIsNullException();
		}
		this.fieldName = fieldName;
		this.operator = operator;
	}

	protected boolean isEqualTo(VDatabaseField another) {
		if (another == null) {
			return false;
		}
		return (this.fieldName.equalsIgnoreCase(another.fieldName) && this.operator == another.operator);
	}

	@Override
	public String toString(boolean rawValueData, Map<String, String> nameMap)
			throws VFrameException {
		return this.operator.toString(rawValueData, fieldName, nameMap);
	}

}
